MAKEFLAGS = -sR
CCSTR		= 	'CC -[M] 正在构建... '$<
PRINTCSTR 	=	@echo $(CCSTR) 

CC		= $(CROSS_COMPILE)gcc
CPP		= $(CROSS_COMPILE)cpp
LD		= $(CROSS_COMPILE)ld

CFLAGS := -c -lgcc -lgcc_eh
CPPFLAGS := -E -P -lgcc -lgcc_eh
LDFLAGS := -static -T ../pub/app.lds -Map app.map

obj-y = ../pub/crt_start.o main.o ../pub/syscalllib.o ../pub/utils.o

.PHONY: all everything clean mountdir_cp


all: clean everything


everything:$(obj-y) ../pub/app.lds uapp.elf

mountdir_cp:
	sudo losetup -P /dev/loop16 ../../../../dim-sum.img
	sync
	sudo fsck -a -t ext3 /dev/loop16p1
	sync
	sudo mount -t ext3 /dev/loop16p1 ./hdisk/
	sync
	sudo cp ./uapp.elf ./hdisk/
	sync
	sudo umount ./hdisk/
	sudo losetup -d /dev/loop16

clean:
	rm -f ../pub/*.o *.o *.bin *.i *.elf *.lds *.app *.map
	@echo '清理全部已构建文件... ^_^'

%.o : %.c
	$(CC) $(CFLAGS) -o $@ $<
	$(PRINTCSTR)
%.o : %.S
	$(CC) $(CFLAGS) -o $@ $<
	$(PRINTCSTR)	
%.lds : %.S
	$(CC) $(CPPFLAGS) -o $@ $<
	$(PRINTCSTR)
uapp.elf:
	$(LD) $(LDFLAGS) -o $@ $(obj-y)
	echo 'LD -[M] 正在构建... uapp.elf'
